package offer;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Elephas
 * @Date 2022/1/28
 **/
@FunctionalInterface
public interface RestoreIpAddress {
    List<String> restoreIpAddresses(String s);
}

class RestoreIpAddressImpl1 implements RestoreIpAddress{
    static final int THREE = 3;
    static final int NINE = 9;
    static final int TWELVE = 12;
    @Override

    public List<String> restoreIpAddresses(String s) {
        // 考虑平凡情况，
        // s 长度为 4 则其格式必为 a.b.c.d
        // s 长度为 12，其格式必为 abc.def.ghi.jkl
        List<String> stringList = new ArrayList<>();
        if(s.length() == 4){
            StringBuilder ans = new StringBuilder();
            for (int i = 0; i < THREE; i++) {
                ans.append(s.charAt(i));
                ans.append('.');
            }
            ans.append(s.charAt(THREE));
            stringList.add(ans.toString());
        }else if(s.length() == 12){
            StringBuilder ans = new StringBuilder();
            for (int i = 0; i < NINE; i++) {
                if((i+1)%3 == 0){
                    ans.append('.');
                }
                ans.append(s.charAt(i));
            }
            for (int i = NINE; i < TWELVE; i++) {
                ans.append(s.charAt(i));
            }
            stringList.add(ans.toString());
        }


        return stringList;
    }
    private boolean validNumber(String s,int start,int end){
        if(start == end){
            return true;
        }
        if(s.charAt(start) == '0'){
            return false;
        }
        int sum = 0, p = start;
        while(start <= end){
            sum *= 10;
            sum += s.charAt(p) - '0';
        }
        if(sum<=255){
            return true;
        }else {
            return false;
        }
    }
}
